package org.web.base.domain.helper;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.web.base.domain.ResultDO;
import org.web.base.domain.exception.ServiceException;
import org.web.base.domain.exception.ResultMessageEnum;

import java.util.List;
import java.util.Map;

/**
 * 类ServiceExceptionHelper.java的实现描述：
 *
 * @author Administrator 2016年10月24日 上午11:19:35
 */
@SuppressWarnings("rawtypes")
public class ServiceExceptionHelper {

    public static ServiceException buildServiceException(Exception e, ResultMessageEnum resultMessageEnum, String description, List<String> errorList, Map extendsValues) {
        if (e instanceof ServiceException) {
            return (ServiceException) e;
        }
        if (ObjectUtils.isEmpty(resultMessageEnum)) {
            //默认为系统异常
            resultMessageEnum = ResultMessageEnum.SYSTEM_EXCEPTION;
        }

        ServiceException serviceException = new ServiceException(resultMessageEnum.getCode(), description, errorList, extendsValues);
        if (ObjectUtils.isNotEmpty(e)) serviceException.setStackTrace(e.getStackTrace());
        if (StringUtils.isEmpty(serviceException.getDescription()) && StringUtils.isEmpty(serviceException.getMessage()))
            serviceException.setDescription(resultMessageEnum.getMessage());
        return serviceException;
    }

    public static ServiceException buildServiceException(Exception e, ResultMessageEnum resultMessageEnum, String description, List<String> errorList) {
        return buildServiceException(e, resultMessageEnum, description, errorList, null);
    }

    public static ServiceException buildServiceException(Exception e, ResultMessageEnum resultMessageEnum, String description, Map extendsValues) {
        return buildServiceException(e, resultMessageEnum, description, null, extendsValues);
    }

    public static ServiceException buildServiceException(Exception e, ResultMessageEnum resultMessageEnum, String description) {
        return buildServiceException(e, resultMessageEnum, description, null, null);
    }

    public static ServiceException buildServiceException(Exception e, ResultMessageEnum resultMessageEnum) {
        return buildServiceException(e, resultMessageEnum, null, null, null);
    }

    public static ServiceException buildServiceException(ResultMessageEnum resultMessageEnum, String description) {
        return buildServiceException(null, resultMessageEnum, description, null, null);
    }

    public static ServiceException buildServiceException(ResultMessageEnum resultMessageEnum) {
        return buildServiceException(null, resultMessageEnum, null, null, null);
    }

    public static ServiceException buildServiceException(ResultMessageEnum resultMessageEnum, Map extendsValues) {
        return buildServiceException(null, resultMessageEnum, null, null, extendsValues);
    }

    public static ServiceException buildServiceException(ResultMessageEnum resultMessageEnum, List<String> errorList) {
        return buildServiceException(null, resultMessageEnum, null, errorList, null);
    }

    public static ServiceException buildServiceException(Exception e, ResultMessageEnum resultMessageEnum, Map extendsValues) {
        return buildServiceException(e, resultMessageEnum, null, null, extendsValues);
    }

    public static ServiceException buildServiceException(ResultDO resultDO) {
        return new ServiceException(resultDO.getCode(), resultDO.getMessage(), null, resultDO.getExtendsDomain());
    }

    // Object paramValue could be Integer、Long or String
    public static void checkNotNullAndOverMaxLength(String paramName, Object paramValue, int maxLength) {
        checkNotNull(paramName, paramValue);
        checkOverMaxLength(paramName, paramValue, maxLength);
    }

    public static void checkNotNull(String paramName, Object paramValue) {
        if (paramValue == null) {
            throw buildServiceException(ResultMessageEnum.PARAM_EMPTY, paramName + " is null.");
        }
    }


    public static void checkOverMaxLength(String paramName, Object paramValue, int maxLength) {
        String temp = String.valueOf(paramValue);
        if (temp != null && temp.length() > maxLength) {
            buildDataOverMaxLength(paramName, temp, maxLength);
        }
    }

    private static ServiceException buildDataOverMaxLength(String paramName, Object paramValue, int maxLength) {
        ServiceException serviceException = buildServiceException(ResultMessageEnum.DATA_OUT_OF_RANGE, paramName + "'s length is more than max value " + maxLength);
        serviceException.withExtendsValue(paramName, paramValue);
        return serviceException;
    }

    public static String getExceptionInfo(Exception e) {
        StringBuilder buffer = new StringBuilder();
        if (null != e && e instanceof ServiceException) {
            ServiceException serviceException = (ServiceException) e;
            buffer.append(serviceException.getDescription()).append("\n\t");
        }
        if (e != null) {
            buffer.append(e.toString()).append("\n\t");
            StackTraceElement[] messages = e.getStackTrace();
            for (StackTraceElement message : messages) {
                buffer.append("at ").append(message.toString()).append("\n\t");
            }
        }
        return buffer.toString();
    }
}
